<?php

namespace application\diyform\controller;

use application\admin\controller\base as auth;
use ticky\request;
use ticky\schema;

//diyform/diyform/manage

class diyform extends auth {

    public function index() {
        $search = array('type' => 1);
        $ret = db('model')->where($search)->order('modelid desc')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->display('diyform_list');
    }

    public function add() {
        if (request::isPost()) {
            $data = $this->post_frm();
            if (db()->table_exists($data['tablename'])) {
                showmsg("表名已存在！");
            }
            db('model')->insert($data);
            schema::sql_create($data['tablename']);
            showmsg('添加成功', '/diyform/diyform');
        } else {
            $this->assign('postUrl', '/diyform/diyform/add');
            $this->assign('action', '添加');
            $item['show_template'] = 'show_diyform';
            $item['allowvisitor'] = '0';
            $item['check_code'] = '0';

            $show_temp = select_template('show_temp', 'show_');
            $this->assign('show_temp', $show_temp);
            $this->assign('item', $item);
            $this->display('diyform_manage');
        }
    }

    public function update() {
        $modelid = request::get('modelid', 0);
        if (request::isPost()) {
            $model = db('model')->where(['modelid' => $modelid])->find();
            $data = $this->post_frm();
            $data['tablename'] = $model['tablename'];
            db('model')->where(['modelid' => $modelid])->update($data);
            showmsg('更新成功', '/diyform/diyform');
        } else {
            $item = db('model')->where(['modelid' => $modelid])->find();
            $setting = json_decode($item['setting'], true);
            $item = array_merge($item, $setting);
            $this->assign('postUrl', '/diyform/diyform/update');
            $this->assign('action', '修改');

            $show_temp = select_template('show_temp', 'show_');
            $this->assign('show_temp', $show_temp);
            $this->assign('item', $item);
            $this->display('diyform_manage');
        }
    }

    // 删除记录
    public function delete() {
        $modelid = request::get('modelid', 0);
        $r = db('model')->field('tablename')->where(array('modelid' => $modelid))->find();
        if ($r) {
            schema::sql_delete($r['tablename']);
        }
        db('model')->delete(['modelid' => $modelid]); //删除model信息
        db('model_field')->delete(['modelid' => $modelid]);  //删除字段
        showmsg('删除成功', '/diyform/diyform');
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['siteid'] = $this->siteid;
        $arr['name'] = request::post('name', '');
        $arr['tablename'] = request::post('tablename', '');
        $arr['description'] = request::post('description', '');
        $arr['inputtime'] = TIMESTAMP;
        $arr['items'] = request::post('items', '');
        $arr['disabled'] = request::post('disabled', '1');
        $arr['type'] = 1;
        $arr['sort'] = request::post('sort', '0');
        $arr['issystem'] = 0;
        $arr['setting'] = json_encode(array(
            'show_template' => request::post('show_template', ''),
            'check_code' => request::post('check_code', '0'),
            'sendmail' => request::post('sendmail', '0'),
            'allowvisitor' => request::post('allowvisitor', '0')
        ));
        return $arr;
    }

}
